<?php 
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * Build binary tree for Calculate Expressions
 * 
 * @package    questions
 * @category   Calculate Expression
 * @copyright  2014 Quoc Dai
 * @author	   Quoc Dai, Volgograd State Technical University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
 */

global $CFG;
require_once($CFG->dirroot.'/question/type/cppexpression/qtype_cppexpression_create_tree.php');


/**
 * generate html-table (with color-code 0 and 1) on the basis of the expression as a string
 * @param string $expression
 * @param int $N
 * @param int $M
 * @return no return
*/
function qtype_cppexpression_array2d_simple_hint($expression, $N, $M)
{
	$binaryTree = new qtype_cppexpression_create_tree($expression);
	$tree = $binaryTree->get_binary_tree();

	echo "<div style = \"font-size: 30px; text-align: center; color: red; margin-bottom: 20px; margin-top: 100px;\"> function : qtype_cppexpression_array2d_simple_hint  </div>";
		echo "<div style = \"font-size: 30px; background: #64f4e3; text-align: center;padding-top: 20px; padding-bottom: 10px;\">";
	for ($i=0; $i < $N; $i++) {
		echo "<div style = \"margin-bottom: 5px;\">";
		for ($j=0; $j < $M; $j++) { 
			if ($tree->calculate_expression($i, $j) == 0) {
				echo "<span style = \"color: #121ab5; margin-left: 25px; \">0</span>";
			}
			else
			{
				echo "<span style = \"color: #d711db; margin-left: 25px; \">1</span>";
			}
			//echo $expression[$i][$j] . "   ";
		}
		echo "</div>";
	}
	echo "</div>";
}


/**
 * generate html-table , in which cells with identical values are white color and different - highlighted values are listed by praction
 * @param string $expression1
 * @param string $expression2
 * @param int $N
 * @param int $M
 * @return no return
*/
function qtype_cppexpression_array2d_complex_hint($expression1, $expression2, $N, $M)
{
	$binaryTree1 = new qtype_cppexpression_create_tree($expression1);
	$tree1 = $binaryTree->get_binary_tree();

	$binaryTree2 = new qtype_cppexpression_create_tree($expression2);
	$tree2 = $binaryTree->get_binary_tree();

	echo "<div style = \"font-size: 30px; color: red; margin-bottom: 20px; margin-top: 100px;\"> function : qtype_cppexpression_array2d_complex_hint</div>";
	echo "<div style = \"font-size: 30px; background: #0cd1cd; text-align: center;padding-top: 20px; padding-bottom: 10px;\">";
	echo "<div style = \"margin: 0px auto;\">";
	for ($i=0; $i < $N; $i++) { 
		echo "<div style = \"margin: 0px auto 5px auto;\">";
		for ($j=0; $j < $M; $j++) { 
			if ($tree1->calculate_expression($i, $j) == $tree2->calculate_expression($i, $j)) {
				echo "<div style = \"color: #ffffff; margin-left: 5px; width: 40px; text-align: center; float: left;\">". $expression1[$i][$j] ."</div>";
			}
			else
			{
				echo "<div style = \"margin-left: 5px; width: 40px; text-align: center; float: left;\">". "<span style = \"color: #121ab5;\">" . $expression1[$i][$j] . "</span>" . "<span style = \"color: #fff;\">/</span>". "<span style = \"color: #d711db;\">" . $expression2[$i][$j] ."</span>" ."</div>";
			}
		}
		echo "<div style = \"clear: both;\"></div>";
		echo "</div>";
	}
	echo "</div>";
	echo "</div>";
}


/**
 * compare results of 2 expressions
 * @param string $expression1
 * @param string $expression2
 * @param int $N
 * @param int $M
 * @return true or false
*/
function qtype_cppexpression_array2d_grade($expression1, $expression2, $N, $M)
{
	$binaryTree1 = new qtype_cppexpression_create_tree($expression1);
	$tree1 = $binaryTree->get_binary_tree();

	$binaryTree2 = new qtype_cppexpression_create_tree($expression2);
	$tree2 = $binaryTree->get_binary_tree();

	for ($i=0; $i < $N; $i++) { 
		for ($j=0; $j < $M; $j++) { 
			if ($tree1->calculate_expression($i, $j) !== $tree2->calculate_expression($i, $j)) {
				return false;
			}
		}
	}

	return true;
}

?>